package com.ff.xyh.access.service.impl.complaint;

import com.ff.xyh.access.service.complaint.ComplaintFeignService;
import com.ff.xyh.common.entity.complaint.ComplaintHandle;
import com.ff.xyh.common.entity.complaint.ComplaintInfo;
import com.ff.xyh.common.entity.dictionary.ComplaintStatus;
import com.ff.xyh.common.entity.dictionary.ComplaintType;
import com.ff.xyh.common.entity.dictionary.Type;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.userfront.FrontUser;
import com.ff.xyh.common.qo.complaint.ComplaintHandleQuery;
import com.ff.xyh.common.qo.complaint.ComplaintInfoQuery;
import com.ff.xyh.common.utils.CommonUtil;
import com.ff.xyh.common.vo.complaint.ComplaintInfoVo;
import com.ff.xyh.feign.client.complaint.ComplaintClient;
import com.ff.xyh.feign.client.dictionary.ComplaintStatusClient;
import com.ff.xyh.feign.client.dictionary.ComplaintTypeClient;
import com.ff.xyh.feign.client.dictionary.TypeClient;
import com.ff.xyh.feign.client.userfront.FrontUserClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther FF
 * @Date 2023年 05月 24日
 * @Time 03:24
 */


@Service
public class ComplaintFeignServiceImpl implements ComplaintFeignService {

    @Autowired
    private ComplaintClient complaintClient;
    @Autowired
    private ComplaintTypeClient complaintTypeClient;
    @Autowired
    private ComplaintStatusClient complaintStatusClient;
    @Autowired
    private TypeClient typeClient;

    @Autowired
    private FrontUserClient frontUserClient;


    @Override
    public ResultObject getComplaintInfoCondition(ComplaintInfoQuery complaintInfoQuery) {
        ResultObject resultObject = complaintClient.getComplaintInfoCondition(complaintInfoQuery);
        List<ComplaintInfo> list = CommonUtil.getItemsInResultObject(resultObject, ComplaintInfo.class);
        if (list == null || list.isEmpty()) return resultObject;

        List<ComplaintInfoVo> voList = new ArrayList<>();

        List<ComplaintType> allComplaintTypes = CommonUtil.getItemsInResultObject(complaintTypeClient.getAllComplaintType(), ComplaintType.class);
        List<ComplaintStatus> allComplaintStatus = CommonUtil.getItemsInResultObject(complaintStatusClient.getAllComplaintStatus(), ComplaintStatus.class);
        List<Type> allTypes = CommonUtil.getItemsInResultObject(typeClient.getAllType(), Type.class);

        List<String> userIds = list.stream().map(ComplaintInfo::getUserId).collect(Collectors.toList());
        List<FrontUser> complaintUserList = CommonUtil.getItemsInResultObject(frontUserClient.getUserBaseInfosByIds(userIds), FrontUser.class);

        list.forEach(complaintInfo -> {
            ComplaintInfoVo complaintInfoVo = new ComplaintInfoVo();
            BeanUtils.copyProperties(complaintInfo, complaintInfoVo);

            String complaintTypeStr = null;
            String complaintStatusStr = null;
            String typeStr = null;
            String userNickname = null;
            if (allComplaintTypes != null && !allComplaintTypes.isEmpty()) {
                for (ComplaintType complaintType : allComplaintTypes) {
                    if (complaintType.getId() == complaintInfo.getComplaintType()) {
                        complaintTypeStr = complaintType.getType();
                        break;
                    }
                }
            }
            if (allComplaintStatus != null && !allComplaintStatus.isEmpty()) {
                for (ComplaintStatus complaintStatus : allComplaintStatus) {
                    if (complaintStatus.getId() == complaintInfo.getStatus()) {
                        complaintStatusStr = complaintStatus.getStatus();
                        break;
                    }
                }
            }
            if (allTypes != null && !allTypes.isEmpty()) {
                for (Type type : allTypes) {
                    if (type.getId() == complaintInfo.getRelatedType()) {
                        typeStr = type.getType();
                        break;
                    }
                }
            }

            if (complaintUserList != null && !complaintUserList.isEmpty()) {
                for (FrontUser user : complaintUserList) {
                    if (complaintInfo.getUserId().equals(user.getId())) {
                        userNickname = user.getNickname();
                        break;
                    }
                }
            }

            if (! (StringUtils.isEmpty(complaintTypeStr)
                    || StringUtils.isEmpty(complaintStatusStr)
                    || StringUtils.isEmpty(typeStr)
            )) {
                voList.add(complaintInfoVo
                        .setComplaintType(complaintTypeStr)
                        .setStatus(complaintStatusStr)
                        .setUserNickname(StringUtils.isEmpty(userNickname)
                                ? "用户已注销": userNickname)
                        .setRelatedType(typeStr));
            }
        });
        return ResultObject.ok().changeList(voList);
    }

    @Override
    public ResultObject deleteComplaintInfoById(String id) {
        return complaintClient.deleteComplaintInfoById(id);
    }

    @Override
    public ResultObject addComplaintInfo(ComplaintInfo complaintInfo) {
        return complaintClient.addComplaintInfo(complaintInfo);
    }

    @Override
    public ResultObject modifyComplaintInfo(ComplaintInfo complaintInfo) {
        return complaintClient.modifyComplaintInfo(complaintInfo);
    }

    @Override
    public ResultObject getComplaintHandleCondition(ComplaintHandleQuery complaintHandleQuery) {
        return complaintClient.getComplaintHandleCondition(complaintHandleQuery);
    }

    @Override
    public ResultObject deleteComplaintHandleById(String id) {
        return complaintClient.deleteComplaintHandleById(id);
    }

    @Override
    public ResultObject addComplaintHandle(ComplaintHandle complaintHandle) {
        return complaintClient.addComplaintHandle(complaintHandle);
    }

    @Override
    public ResultObject modifyComplaintHandle(ComplaintHandle complaintHandle) {
        return complaintClient.modifyComplaintHandle(complaintHandle);
    }

    @Override
    public ResultObject handleComplaintInfo(ComplaintHandle complaintHandle) {
        if (complaintHandle.getStatus() < 1) { // 不可设置为0 或 -1
            return ResultObject.error().message(ResultMessage.ERROR_ARG);
        } else {
            return complaintClient.handleComplaintInfo(complaintHandle);
        }
    }

}